﻿//-----------------------------------------------------------------------
// <copyright file="UpDownButton.xaml.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Controls
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;

    using ChatBox.XamlUtility;

    /// <summary>
    /// Interaction logic for UpDownButton.xaml
    /// </summary>
    public partial class UpDownButton : UserControl
    {
        /// <summary>
        /// Dependency property for IsUp
        /// </summary>
        public static readonly DependencyProperty IsUpProperty =
            DependencyProperty.Register(
            "IsUp", typeof(bool), typeof(UpDownButton), new PropertyMetadata(true));
        
        /// <summary>
        /// the tooltip to use when the arrow is pointing up
        /// </summary>
        private string toolTipUp;

        /// <summary>
        /// the tooltip to use when the arrow is pointing down
        /// </summary>
        private string toolTipDown;

        /// <summary>
        /// Animation for rotating down
        /// </summary>
        private Storyboard rotateDownAnimation;

        /// <summary>
        /// Animation for rotating up
        /// </summary>
        private Storyboard rotateUpAnimation;

        /// <summary>
        /// Initializes a new instance of the UpDownButton class
        /// </summary>
        public UpDownButton()
        {
            InitializeComponent();

            this.rotateDownAnimation = this.FindStoryboard("rotateDownAnimation");
            this.rotateUpAnimation = this.FindStoryboard("rotateUpAnimation");
        }

        /// <summary>
        /// For when Up was clicked
        /// </summary>
        public event EventHandler<EventArgs> UpClicked;

        /// <summary>
        /// For when down was clicked
        /// </summary>
        public event EventHandler<EventArgs> DownClicked;

        /// <summary>
        /// Gets or sets the tooltip to use when the arrow is pointing up
        /// </summary>
        public string ToolTipUp
        {
            get
            {
                return this.toolTipUp;
            }

            set
            {
                this.toolTipUp = value;
                if (this.IsUp)
                {
                    this.ToolTip = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the tooltip to use when the arrow is pointing down
        /// </summary>
        public string ToolTipDown
        {
            get
            {
                return this.toolTipDown;
            }

            set
            {
                this.toolTipDown = value;
                if (! this.IsUp)
                {
                    this.ToolTip = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the arrow is pointing up
        /// </summary>
        public bool IsUp
        {
            get
            {
                return (bool)this.GetValue(IsUpProperty);
            }

            set
            {
                if (this.IsUp != value)
                {
                    this.RotateAround();
                    this.SetValue(IsUpProperty, value);

                    if (value)
                    {
                        this.ToolTip = this.ToolTipUp;
                    }
                    else
                    {
                        this.ToolTip = this.ToolTipDown;
                    }
                }
            }
        }

        /// <summary>
        /// Button click handler
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the event args</param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.IsUp = !this.IsUp;
        }

        /// <summary>
        /// Rotate around to face the other way
        /// </summary>
        private void RotateAround()
        {
            if (this.IsUp)
            {
                this.DoUpClicked();
                this.rotateDownAnimation.Begin();
            }
            else
            {
                this.DoDownClicked();
                this.rotateUpAnimation.Begin();
            }
        }

        /// <summary>
        /// Call the up clicked event handler
        /// </summary>
        private void DoUpClicked()
        {
            if (this.UpClicked != null)
            {
                this.UpClicked(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Call the down clicked event handler
        /// </summary>
        private void DoDownClicked()
        {
            if (this.DownClicked != null)
            {
                this.DownClicked(this, EventArgs.Empty);
            }
        }
    }
}
